package bTree.LeetCode;

import java.util.Arrays;

public class 数组元素相等转换 {
    public boolean canMakeEqual(int[] nums, int k) {
        int length = nums.length;
        int[] nums1 = Arrays.copyOf(nums, length);
        int[] nums2 = Arrays.copyOf(nums, length);
        return check(nums1,1,k)||check(nums2,-1,k);
    }

    private boolean check(int[] nums1, int num, int k) {
        int cnt=0;
        for (int i = 0; i < nums1.length - 1; i++) {
            if (nums1[i]!=num){
                nums1[i]=-nums1[i];
                nums1[i+1]=-nums1[i+1];
                cnt++;
            }
        }
        return nums1[nums1.length-1]==num&&cnt<=k;
    }
}